import 'package:intl/intl.dart';
import 'package:mybaseplus/base/tool/empty/FFEmpty.dart';
import 'package:mybaseplus/base/tool/extension/FFStringExt.dart';
import 'package:mybaseplus/base/tool/pprint/FFLog.dart';

class FFTime {
  ///求现在的时间
  static DateTime now() {
    return DateTime.now();
  }

  ///求现在的时间秒数
  static int nowSecond() {
    return dateToTimestampSecond(DateTime.now());
  }

  ///求现在的时间毫秒数
  static int nowMillSecond() {
    return dateToTimestampMill(DateTime.now());
  }

  ///求今天最后的时间点
  static DateTime todayEndDate() {
    DateTime todayDate = DateTime.now();
    //加上一天，再减去尾巴
    DateTime todayEndDate = todayDate.add(const Duration(days: 1)).subtract(Duration(
          hours: todayDate.hour,
          minutes: todayDate.minute,
          seconds: todayDate.second + 1,
          milliseconds: todayDate.millisecond,
          microseconds: todayDate.microsecond,
        ));

    return todayEndDate;
  }

  ///求今天开始的时间点
  static DateTime todayBeginDate() {
    DateTime todayDate = DateTime.now();
    //减去尾巴
    DateTime todayEndDate = todayDate.subtract(Duration(
      hours: todayDate.hour,
      minutes: todayDate.minute,
      seconds: todayDate.second,
      milliseconds: todayDate.millisecond,
      microseconds: todayDate.microsecond,
    ));

    return todayEndDate;
  }

  ///格式化：一个日期开始的时间点
  static DateTime todayBeginDateByDate(DateTime date) {
    //减去尾巴
    DateTime todayEndDate = date.subtract(Duration(
      hours: date.hour,
      minutes: date.minute,
      seconds: date.second,
      milliseconds: date.millisecond,
      microseconds: date.microsecond,
    ));

    return todayEndDate;
  }

  ///格式化：一个日期的最后的时间点
  static DateTime todayEndDateByDate(DateTime date) {
    //加上一天，再减去尾巴
    DateTime todayEndDate = date.add(const Duration(days: 1)).subtract(Duration(
          hours: date.hour,
          minutes: date.minute,
          seconds: date.second + 1,
          milliseconds: date.millisecond,
          microseconds: date.microsecond,
        ));

    return todayEndDate;
  }

  ///格式化：一个日期所在自然周的星期一的开始的时间点
  static DateTime weekBeginByDate(DateTime date) {
    int dayOfWeek = date.weekday;
    DateTime week1 = date.subtract(Duration(days: dayOfWeek - 1));
    return todayBeginDateByDate(week1);
  }

  ///格式化：一个日期所在自然周的星期日的开始的时间点
  static DateTime weekEndByDate(DateTime date) {
    int dayOfWeek = date.weekday;
    DateTime week1 = date.add(Duration(days: 7 - dayOfWeek));
    return todayBeginDateByDate(week1);
  }

  ///格式化：一个日期所在自然月的第一天的开始的时间点
  static DateTime monthBeginByDate(DateTime date) {
    DateTime monthBegin = DateTime(date.year, date.month, 1);
    return monthBegin;
  }

  ///格式化：一个日期所在自然月的最后一天的最后的时间点
  static DateTime monthEndByDate(DateTime date) {
    DateTime monthBegin = DateTime(date.year, date.month + 1, 0, 23, 59, 59);
    return monthBegin;
  }

  ///格式化：一个日期所在年的第一天的开始的时间点
  static DateTime yearBeginByDate(DateTime date) {
    DateTime monthBegin = DateTime(date.year, 1, 1);
    return monthBegin;
  }

  ///格式化：一个日期所在年的最后一天的最后的时间点
  static DateTime yearEndByDate(DateTime date) {
    DateTime yearEnd = DateTime(date.year + 1, 1, 1).subtract(const Duration(days: 1));
    return todayEndDateByDate(yearEnd);
  }

  ///日期加减
  ///subtract：true：减，false：加
  static DateTime dateByDate(DateTime date, Duration duration, {bool? subtract = true}) {
    if (subtract == true) {
      return date.subtract(duration);
    }

    return date.add(duration);
  }

  ///两个日期是否是同一天
  static bool dateSameDay(DateTime date1, DateTime date2) {
    bool same = false;
    if (date1.year == date2.year &&
        date1.month == date2.month &&
        date1.day == date2.day) {
      same = true;
    }
    return same;
  }

  ///两个时间戳是否是同一天
  static bool timeStampIsSameDay(int second1, int second2) {
    DateTime date1 = secondToDate(second1);
    DateTime date2 = secondToDate(second2);

    return dateSameDay(date1, date2);
  }

  ///比较两个DateTime，相差多少天
  ///返回值
  /// > 0:  date1 时间线在后几天，
  /// < 0:  date1 时间线在前几天，
  /// = 0： 同一天
  static int dateDiff(DateTime date1, DateTime date2) {
    Duration difference = date1.difference(date2);
    return difference.inDays;
  }

  ///比较两个second，相差多少天
  ///返回值
  /// > 0:  second1 时间线在后几天，
  /// < 0:  second1 时间线在前几天，
  /// = 0： 同一天
  static int dateDiffBySecond(int second1, int second2) {
    return dateDiff(secondToDate(second1), secondToDate(second2));
  }

  ///下面都是转换操作
  ///下面都是转换操作
  ///下面都是转换操作

  ///时期转时间戳 毫秒
  static int dateToTimestampMill(DateTime date) {
    return date.millisecondsSinceEpoch;
  }

  ///时期转时间戳 秒
  static int dateToTimestampSecond(DateTime date) {
    return date.millisecondsSinceEpoch ~/ 1000;
  }

  ///毫秒转日期
  static DateTime millToDate(int milliseconds) {
    return DateTime.fromMillisecondsSinceEpoch(milliseconds);
  }

  ///秒转日期
  static DateTime secondToDate(int second) {
    return DateTime.fromMillisecondsSinceEpoch(second * 1000);
  }

  ///日期字符串 转 日期对象
  static DateTime? strDateToDate(String strDate) {
    try {
      return DateTime.parse(strDate);
    } catch (e) {
      FFLog.error("解析出错 e = ${e.toString()}");
    }
    return null;
  }

  ///日期格式化
  static String dateFormat(DateTime date, {String format = "yyyy-MM-dd HH:mm:ss"}) {
    return DateFormat(format).format(date);
  }

  ///日期格式化
  static String dateFormatBySeconds(int second, {String format = "yyyy-MM-dd HH:mm:ss"}) {
    return DateFormat(format).format(secondToDate(second));
  }

  ///毫秒转分钟(带小数)
  static double millToMin(int mill) {
    int second = mill ~/ 1000;
    return second / 60;
  }

  ///毫秒转分钟(带小数)
  static double millToMinStr(String? mill) {
    if (FFEmpty.stringIsEmpty(mill)) {
      return 0.0;
    }

  
    int second = mill.toINT() ~/ 1000;
    return second / 60;
  }
}
